package com.haliyoo.adhere.activity;

import java.io.Serializable;

import com.haliyoo.adhere.R;
import com.haliyoo.adhere.bean.WeiboBean;
import com.haliyoo.adhere.controller.AdUserConstant;
import com.haliyoo.adhere.controller.Constants;
import com.haliyoo.adhere.controller.OAuthConstant;
import com.haliyoo.adhere.util.BitmapGet;
import com.haliyoo.adhere.util.DateUtilsDef;
import com.haliyoo.adhere.widget.AsyncImageLoader;
import com.haliyoo.adhere.widget.FaceManager;
import com.haliyoo.adhere.widget.LocalWeiboLayout;
import com.haliyoo.adhere.widget.WeiboLayout;
import com.haliyoo.adhere.widget.AsyncImageLoader.ImageCallback;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import weibo4android.Count;
import weibo4android.Paging;
import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.SpannableString;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class CreativeActivity extends Activity {
	private static String TAG = "CreativeActivity";
    private long key = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
    	this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.creative);
		
		//获取上一个页面传递过来的key，key为某一条微博的id
        Intent i = this.getIntent();
        if (!i.equals(null)){
            Bundle b=i.getExtras();
            if(b!=null){
                if(b.containsKey("key")){
                    key = b.getLong("key");
                    initParams(key);
                }
            }
        }
        
        /**
         * 用户信息栏事件处理
         */
        RelativeLayout rlNext = (RelativeLayout) findViewById(R.id.tweet_profile);
        rlNext.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int nMyCreative = 2;
				int nMyCharm = 25;
				int nMyContributions = 63;
				int nMyColleagues = 11;
				Intent intent = new Intent();
				Weibo weibo = OAuthConstant.getInstance().getWeibo();
				
				weibo.setToken(OAuthConstant.getInstance().getToken(), OAuthConstant.getInstance().getTokenSecret());
				
				Status status = null;
				try {
					status = weibo.showStatus(key);
				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (status != null) {

					intent.putExtra("title", getString(R.string.STR_COLLEAGUES_CHARM));
					Bundle bundle = new Bundle();
					bundle.putSerializable("user", status.getUser());
					intent.putExtras(bundle);
					intent.setClass(CreativeActivity.this, MyCharmActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(CreativeActivity.this, R.string.STR_NONE_USER, Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		/**
		 * 返回键处理
		 */
		ImageButton ibBack = (ImageButton) findViewById(R.id.IMAGE_BUTTON_BACK);
		ibBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();  
			}
		});
	}

	/**
	 * 加载微博内容
	 * @param id 微博ID
	 */
	private void initParams(final long weiboId) {
		/// 根据id获取微博
		WeiboBean wb = WeiboLayout.getWeiboBean(weiboId);
		
		/// 用户名
		TextView tvUser = (TextView) findViewById(R.id.tweet_profile_name);
		tvUser.setText(wb.getUser());
		
		/// 头像
		ImageView ivProfile = (ImageView) findViewById(R.id.tweet_profile_preview);
    	BitmapGet.showImage(ivProfile, wb.getProfileUrl(), wb.getProfile());
		
		/// 微博内容
		TextView tvText = (TextView) findViewById(R.id.tweet_message);
		SpannableString ss = FaceManager.getInstance().convertTextToEmotion(this, wb.getText());
		tvText.setText(ss);
		
		/// 图片
        if(wb.getExistImage()) {
            ImageView pic = (ImageView) findViewById(R.id.tweet_upload_pic);
            pic.setVisibility(View.VISIBLE);
        	BitmapGet.showImage(pic, wb.getIconUrl(), wb.getIcon());
        }
		
		/// 定位状态
        ImageView ivLocation = (ImageView) findViewById(R.id.tweet_gps_image);
		if (wb.getExistLocation()) {
			ivLocation.setVisibility(View.VISIBLE);
			ivLocation.setImageResource(R.drawable.location_icon);
		} else {
			ivLocation.setVisibility(View.GONE);
		}
		
		/// 转播
		if (wb.getRetweet() != null) {
	        LinearLayout llRepost = (LinearLayout) findViewById(R.id.src_text_block);
	        llRepost.setVisibility(View.VISIBLE);
	        
			TextView tvTextRepost = (TextView) findViewById(R.id.tweet_oriTxt);
			tvTextRepost.setVisibility(View.VISIBLE);
			SpannableString ss2 = FaceManager.getInstance()
					.convertTextToEmotion(
							this,
							"@" + wb.getRetweet().getUser() + ":"
									+ wb.getRetweet().getText());
			tvTextRepost.setText(ss2);

			/// 图片
	        if(wb.getRetweet().getExistImage()) {
	            ImageView pic = (ImageView) findViewById(R.id.tweet_upload_pic2);
	            pic.setVisibility(View.VISIBLE);
	        	BitmapGet.showImage(pic, wb.getRetweet().getIconUrl(), wb.getRetweet().getIcon());
	        }
			
			/// 转发
			TextView tvRepost = (TextView) findViewById(R.id.tweet_redirect_rt);
			tvRepost.setText(wb.getRetweet().getRepostCount() + "");
			tvRepost.setVisibility(View.VISIBLE);
			
			/// 评论
			TextView tvConment = (TextView) findViewById(R.id.tweet_comment_rt);
			tvConment.setText(wb.getRetweet().getCommentCount() + "");
			tvConment.setVisibility(View.VISIBLE);
		}
		
		/// 转发
		Button btRepost = (Button) findViewById(R.id.tweet_redirect);
		btRepost.setText(wb.getRepostCount() + "");
		btRepost.setVisibility(View.VISIBLE);
		btRepost.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				
				intent.putExtra("cmd", "repost");
				intent.putExtra("weiboId", weiboId);
				
				intent.setClass(CreativeActivity.this, CreativeDesignActivity.class);
				startActivity(intent);
			}
		});
		
		/// 评论
		Button btComment = (Button) findViewById(R.id.tweet_comment);
		btComment.setText(wb.getCommentCount() + "");
		btComment.setVisibility(View.VISIBLE);
		btComment.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				
				intent.putExtra("cmd", "comment");
				intent.putExtra("weiboId", weiboId);
				
				intent.setClass(CreativeActivity.this, CreativeDesignActivity.class);
				startActivity(intent);
			}
		});
		
		/// 更新时间
		TextView tvCreateTime = (TextView) findViewById(R.id.tweet_updated);
		tvCreateTime.setText(wb.getTime().toLocaleString());
		
		/// 来自
		TextView tvComeFrom = (TextView) findViewById(R.id.tweet_via);
		tvComeFrom.setText(getString(R.string.STR_COME_FROM)
				+ wb.getComeString());
	}


	/**
	 * 加载微博内容
	 * @param weiboBean 微博
	 */
	private void loadWeiboContent(WeiboBean weiboBean){
		if (LoginActivity.getUser() == null) {
			Toast.makeText(this, R.string.STR_NONE_USER, Toast.LENGTH_SHORT).show();
	} else {
			if (weiboBean != null) {
				/// 用户名
				TextView tvUser = (TextView) findViewById(R.id.TEXT_VIEW_USER);
				tvUser.setText(weiboBean.getUser());
				
				/// 头像
				ImageView ivProfile = (ImageView) findViewById(R.id.IMAGE_VIEW_PROFILE);
                AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
				Drawable cachedImage = asyncImageLoader.loadDrawable(
						weiboBean.getProfileUrl().toString(), 
						ivProfile, new ImageCallback() {

					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUrl) {
						imageView.setImageDrawable(imageDrawable);
					}

				});
				if (cachedImage == null) {
					ivProfile.setImageResource(R.drawable.icon);
				} else {
					ivProfile.setImageDrawable(cachedImage);
				}
				
				/// 微博内容
				TextView tvText = (TextView) findViewById(R.id.TEXT_VIEW_TEXT);
				tvText.setText(weiboBean.getText());
				
				/// 图片
                if(weiboBean.getExistImage()){
                    Bitmap bitmap = weiboBean.getIcon();

                    ImageView pic = (ImageView) findViewById(R.id.IMAGE_VIEW_PIC);
                    pic.setImageBitmap(bitmap);
                    /*pic.setOnClickListener(new OnClickListener() {

                    	public void onClick(View v) {
                            Object obj=v.getTag();
                            Intent intent = new Intent();
                            Bundle b=new Bundle();
                            b.putString("url", obj.toString());
                            intent.putExtras(b);
                            intent.setClass(CreativeActivity.this,ImageActivity.class);
                            startActivity(intent);
                        }
                    });*/
                }
			}
        }
	}
}
